package com.gitee.wsl.common.ui.ext.color

import androidx.compose.ui.graphics.Color

/** Returns the version of this color based on the specified alpha. */
fun Color.withAlpha(alpha: Float): Color {
    if (alpha == 1.0f) {
        return this
    }
    return Color(this.red, this.green, this.blue, alpha, this.colorSpace)
}

/** Returns the version of this color based on the specified alpha. */
fun Color.byAlpha(alpha: Float): Color {
    return Color(this.red, this.green, this.blue, this.alpha * alpha, this.colorSpace)
}


private const val ALPHA_BIT_SHIFT = 24
private const val COLOR_MASK = 0xff

val Int.alpha: Int
    get() = extractColorChannel(ALPHA_BIT_SHIFT)

private fun Int.extractColorChannel(bitShift: Int): Int = this shr bitShift and COLOR_MASK

val Long.alpha: Float
    get() =
        if (this and 0x3fL == 0L) (this shr 56 and 0xff) / 255.0f else (this shr 6 and 0x3ff) / 1023.0f
